package saucaca.fragment;

import java.util.ArrayList;

import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.Adapter.RingtoneAdapter;
import saucaca.CustomView.XListView;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.RingtoneObject;
import saucaca.Object.TelcoObject;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class RingToneListTone extends Fragment implements saucaca.CustomView.XListView.IXListViewListener {
	public static final String TAG = RingToneListTone.class.getSimpleName();
	ArrayList<RingtoneObject> ringtones;
	MainActivity mActivity;
	private XListView expandListview;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	boolean isFromMenu;
	TextView txtTitle;
	String telCODE = "";

	String cateCode = "";
	String singerCode = "";
	String authorCode = "";
	String areaCode = "";
	String type = "0";
	String page = "0";
	String pageSize = "";
	int xpage = 0;
	boolean loadingMore;
	private SwipeRefreshLayout mSwipeLayout;
	
	RingtoneAdapter adapter;
	
	public RingToneListTone(TelcoObject telcode) {
		telCODE = telcode.getCODE();
	}

	boolean checkString(String str) {
		if (str != null && !str.equals("")) {
			return true;
		} else
			return false;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.listview, container, false);
		findviewByID(android);
		new GetFromServices().execute("");
		return android;
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void findviewByID(View view) {
		expandListview = (XListView) view.findViewById(R.id.layoutlistview_listview);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		expandListview.setEmptyView(mView_EmptyLoading);
		expandListview.setPullLoadEnable(true);
		expandListview.setXListViewListener(this);
		expandListview.startProgressBar();
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
		mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				loadingMore = false;
				expandListview.stopProgressBar();
				stop();
				new GetFromServices().execute("");
			}
		});
		mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
				android.R.color.holo_green_light, android.R.color.holo_orange_light,
				android.R.color.holo_red_light);
	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... param) {
			page = Integer.toString(xpage);
			loadingMore = true;
			String str = WServiceLib.getListRingtone(page, pageSize, type, cateCode, singerCode, authorCode, areaCode, telCODE);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					ArrayList<RingtoneObject> listringtone = JsonReader.readList_ringtone(result);
					if(adapter==null){
						ringtones= JsonReader.readList_ringtone(result);
						adapter = new RingtoneAdapter(mActivity, ringtones);
						expandListview.setAdapter(adapter);
					}else{
						if(listringtone.size()>0)
						for (int i = 0; i < listringtone.size(); i++) {
							boolean isAdded=false;
							for (int j = 0; j < ringtones.size(); j++) {
								if(ringtones.get(j).getSONG_ID().equals(listringtone.get(i).getSONG_ID()))
									isAdded=true;
							}
							if(!isAdded)
								ringtones.add(listringtone.get(i));
						}
					}
						
					if (ringtones.size() == 0) {
						progressBar.setVisibility(View.GONE);
						imgEmpty.setVisibility(View.VISIBLE);
						txtStatus.setText(mActivity.getResources().getString(R.string.no_item_song));
					}
					loadingMore = false;
					expandListview.stopProgressBar();
					if(mSwipeLayout.isRefreshing())
						mSwipeLayout.setRefreshing(false);
					stop();
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}

	}

	public void stop() {
		expandListview.stopRefresh();
		expandListview.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		if (!loadingMore) {
			xpage += 1;
			new GetFromServices().execute("");
		} else
			stop();
	}

	@Override
	public void onLoadMore() {
		if (!loadingMore) {
			xpage += 1;
			new GetFromServices().execute("");
		} else
			stop();
	}
}
